{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import re\n",
    "import os\n",
    "import codecs\n",
    "from threading import Thread"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://dblp.org/xml/release/\"\n",
    "rsp = requests.get(url)\n",
    "download_url = \"https://dblp.org/xml/release/{}\"\n",
    "items = re.findall(r'\"(dblp-\\S+?.xml.gz)\"', rsp.text)\n",
    "rsp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def download_item(item):\n",
    "    if os.path.exists(\"./data/dlpg/{}\".format(item)):\n",
    "        return\n",
    "    with requests.get(download_url.format(item), stream=True) as response:\n",
    "        chunk_size = 1024*1024  # 单次请求最大值\n",
    "        content_size = int(response.headers['content-length'])  # 内容体总大小\n",
    "        data_count = 0\n",
    "        with open(\"./data/dlpg/{}\".format(item), \"wb\") as file:\n",
    "            for data in response.iter_content(chunk_size=chunk_size):\n",
    "                file.write(data)\n",
    "                data_count = data_count + len(data)\n",
    "                now_jd = (data_count / content_size) * 100\n",
    "                print(\"\\r 文件下载进度：%d%%(%d/%d) - %s\" % (now_jd, data_count, content_size, \"./data/dlpg/{}\".format(item)), end=\" \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!rm -rf ./data/dlpg/*"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      " 文件下载进度：100%(534528044/534528044) - ./data/dlpg/dblp-2019-12-01.xml.gz 12\n",
      " 文件下载进度：82%(392167424/473148403) - ./data/dlpg/dblp-2018-12-01.xml.gz     "
     ]
    }
   ],
   "source": [
    "for i in range(0, len(items), 20):\n",
    "    print(i)\n",
    "    start = i\n",
    "    end = i+20 if i+20 <= len(items) else len(items)\n",
    "    jobs = []\n",
    "    for item in items[start:end]:\n",
    "        jobs.append(Thread(target=download_item, args=(item, )))\n",
    "    for job in jobs:\n",
    "        job.start()\n",
    "    for job in jobs:\n",
    "        job.join()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "63"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
